package com.cn.wanxi.service.impl;

import com.cn.wanxi.Result.Result;
import com.cn.wanxi.entity.User;
import com.cn.wanxi.mapper.UserMapper;
import com.cn.wanxi.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    //    @Autowired
//    private NewsService newsService;
    @Override
    public Result login(User user) {
        if (!user.getSessionCode().equals(user.getCode())) {
            return Result.getInstance("error-code");
        }
        User user1 = userMapper.findByUsernameAndPassword(user);
        if (user1 == null) {
            return Result.getInstance("error");
        }

        return Result.getInstance("success");
    }

    @Override
    public Result findAll(User user) {
        if (user.getHobby() != null && !"".equals(user.getHobby())) {
            user.setHobbys(user.getHobby().split(","));
        }
        PageHelper.startPage(1, 4);
        List<User> list = userMapper.findAll(user);
        PageInfo<User> userPageInfo = new PageInfo<>(list);
        return Result.getInstance(userPageInfo);
    }

    @Override
    public Result del(User user) {
        return null;
    }

    @Override
    public Result add(User user) {
        return Result.getInstance(userMapper.add(user));
    }

    @Override
    public Result findById(User user) {
        return null;
    }

    @Override
    public Result update(User user) {
        return Result.getInstance(userMapper.update(user));
    }

    @Override
    public Result getCount(User user) {
        return Result.getInstance(userMapper.getCount(user));
    }


}
